﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using System.Data;
using ProjectWebNhac.DTO;
using System.Web.Services;

namespace ProjectWebNhac.GUI.User
{
    public partial class BaiHat1 : System.Web.UI.Page
    {
        TheLoaiBus tlbus = new TheLoaiBus();
        int maTheLoai=-1;
        int Pages = 1;
        DataTable dt = new DataTable();
        CaSiBus csbus = new CaSiBus();
        BaiHatBus bhbus = new BaiHatBus();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Request["MaTheLoai"] != null)
            {
                maTheLoai = Convert.ToInt16(Request["MaTheLoai"]);
               
            }
            if (Request["page"] != null)
            {
                Pages = Convert.ToInt16(Request["page"]);
            }
            if (!Page.IsPostBack)
            {
                loadDanhSachTheLoai();
                LoadDanhSachBaiHat();
                if (Session["ThanhVien"]!=null)
                {
                    NguoiDungDTO objUser = (NguoiDungDTO)Session["ThanhVien"];
                    txtUserName.Text = objUser.TaiKhoan;
                }
                loadPlaylist();
            }
        }
        private void loadPlaylist()
        {
            if (Session["ThanhVien"] != null)
            {
                NguoiDungDTO objUser = (NguoiDungDTO)Session["ThanhVien"];
                PlaylistBus plBus = new PlaylistBus();
                dropPlaylist.DataSource = plBus.LayPlaylistTheoTaiKhoan(objUser.TaiKhoan);
                dropPlaylist.DataTextField = "TenPlaylist";
                dropPlaylist.DataValueField = "MaPlaylist";
                dropPlaylist.DataBind();
            }
        }

        private void loadDanhSachTheLoai()
        {
            reTheLoai.DataSource = tlbus.LayTheLoaiNhac();
            reTheLoai.DataBind();
        }

        private void LoadDanhSachBaiHat()
        {
            DataSet ds = bhbus.LayDanhSachBaiHatPT(Pages, 20, 10, maTheLoai);
           
            DataTable dtbData = ds.Tables[0];
          
            if (dtbData.Rows.Count>0)
            {
                DataTable dtbPhanTrang = ds.Tables[1];
                dtbData.Columns.Add("RowIndex", typeof(System.Int32));
                int i = 1;
                foreach (DataRow dr in dtbData.Rows)
                {
                    dr["RowIndex"] = i;
                    i++;
                }
                reBaiHat.DataSource = dtbData;
                reBaiHat.DataBind();
                liPage.Text = dtbPhanTrang.Rows[0][0].ToString();
            }
        }

        [WebMethod(EnableSession = false)]
        public static bool AddPlayList(string MaPL, string MaBaiHat)
        {
            PlaylistBus plBus = new PlaylistBus();

            return plBus.ThemBaiHatVaoPlayList(Convert.ToInt16(MaPL)
                ,Convert.ToInt16(MaBaiHat));
            
        }
        [WebMethod(EnableSession = false)]

        public static string CreateNewPL(string tenPlaylist,string taikhoan)
        {
            PlaylistBus plBus = new PlaylistBus();
            bool isSuccess = true;
            foreach (DataRow row in plBus.LayPlaylistTheoTaiKhoan(taikhoan).Rows)
            {
                string oldName =row["TenPlaylist"].ToString();
                oldName = oldName.TrimEnd();
                oldName = oldName.ToLower();
                if (oldName.Equals(tenPlaylist.ToLower().TrimEnd()))
                {
                    isSuccess = false;
                }
            }
            if (isSuccess)
            {
                isSuccess = plBus.Taomoiplaylist(taikhoan, tenPlaylist);
            }
            
            if (isSuccess)
            {
                return plBus.GetMaxIDPlaylist();
            }
            else
            {
                return "failed";   
            }
        }

        protected void reBaiHat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            int MaBaiHat;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView usrCurrent = e.Item.DataItem as DataRowView;
                MaBaiHat = Convert.ToInt16(usrCurrent["MaBaiHat"]);
                Repeater reDSCaSiBaiHat = (Repeater)e.Item.FindControl("reDSCaSiBaiHat");
                reDSCaSiBaiHat.DataSource = csbus.LayCaSiTheoBaiHatTable(MaBaiHat);
                reDSCaSiBaiHat.DataBind();
            }
        }

        protected void reBaiHat_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "download")
            {
                string linkbai = e.CommandArgument.ToString();
                Response.ContentType = "audio/mp3";
                Response.AddHeader("content-disposition", "attachment; filename=" + linkbai);
                Response.TransmitFile(Server.MapPath("~/Nhac/" + linkbai));
                Response.End();
            }
        }

    }
}
